package bma.common.langutil.pipeline;

/**
 * 通用通道队列构造器（单一）
 * 
 * @author 关中
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <TYPE>
 */
public class CommonPipelineBuilderOne<TYPE> implements
		CommonPipelineBuilder<TYPE> {

	protected String beforeName;
	protected String afterName;
	protected String name;
	protected TYPE item;

	public CommonPipelineBuilderOne() {
		super();
	}

	public CommonPipelineBuilderOne(String name, TYPE h) {
		super();
		this.name = name;
		this.item = h;
	}

	public String getBeforeName() {
		return beforeName;
	}

	public void setBeforeName(String before) {
		this.beforeName = before;
	}

	public String getAfterName() {
		return afterName;
	}

	public void setAfterName(String after) {
		this.afterName = after;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public TYPE getItem() {
		return item;
	}

	public void setItem(TYPE node) {
		this.item = node;
	}

	public CommonPipelineBuilderOne<TYPE> addLast() {
		this.afterName = "*";
		this.beforeName = null;
		return this;
	}

	public CommonPipelineBuilderOne<TYPE> addFirst() {
		this.afterName = null;
		this.beforeName = "*";
		return this;
	}

	public CommonPipelineBuilderOne<TYPE> addBefore(String name) {
		this.afterName = null;
		this.beforeName = name;
		return this;
	}

	public CommonPipelineBuilderOne<TYPE> addAfter(String name) {
		this.afterName = name;
		this.beforeName = null;
		return this;
	}

	@Override
	public void buildPipeline(CommonPipeline<TYPE> pipeline) {
		if (this.afterName != null) {
			if ("*".equals(afterName)) {
				pipeline.addLast(name, item);
			} else {
				pipeline.addAfter(afterName, name, item);
			}
		} else if (this.beforeName != null) {
			if ("*".equals(beforeName)) {
				pipeline.addFirst(name, item);
			} else {
				pipeline.addBefore(beforeName, name, item);
			}
		} else {
			pipeline.addLast(name, item);
		}
	}
}
